﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
using PrimaryKeyFunction;


public partial class Admin_Gas : System.Web.UI.Page
{
    Service objService = new Service();

    Functions objFunction = new Functions();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {

        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillGasComposition(Convert.ToInt32(id));
                txtGasCompId.Text = string.Empty;
                trGasComponentMiscTextBox.Visible  = true;
                trDgGasComposition.Visible = false;
            }

            fillDataGrid();
            fillDataGridMisc();
        }

    }

    private void fillDataGrid()
    {
        string ExpID = (string)Session["ExperimentId"];
        if (ExpID != null)
        {
            if (objService.GetGasComposition(ExpID).Tables[0].Rows.Count > 0)
            {
                dgGasComp.DataSource = objService.GetGasComposition(ExpID);
                dgGasComp.DataBind();
                trTextboxGasComposition.Visible = false;
                trDgGasComposition.Visible = true;
            }
            else
            {
                trTextboxGasComposition.Visible = true;
                trDgGasComposition.Visible = false;
            }

        }
        else
        {
            trTextboxGasComposition.Visible = true;
            trDgGasComposition.Visible = false;
        }
   }

    private void fillDataGridMisc()
    {
        if (Session["GasCompoMisc"] == null)
        {
            if (txtMiscId.Text != string.Empty)
            {
                if (objService.GetGasCompositionMisc(txtMiscId.Text.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgGas.DataSource = objService.GetGasCompositionMisc(txtMiscId.Text.ToString());
                    dgGas.DataBind();
                    trGasComponentMiscDg.Visible = true;
                    trGasComponentMiscTextBox.Visible = false;
                }
                else
                {
                    trGasComponentMiscDg.Visible = false;
                    trGasComponentMiscTextBox.Visible = true;
                }
            }
            else
            {

                string ExpID = (string)Session["ExperimentId"];

                if (ExpID != null)
                {
                    if (objFunction.GetGasCompoMisc(ExpID).Tables[0].Rows.Count > 0)
                    {
                        dgGas.DataSource = objFunction.GetGasCompoMisc(ExpID);
                        dgGas.DataBind();
                        trGasComponentMiscDg.Visible = true;
                        trGasComponentMiscTextBox.Visible = false;
                    }
                    else
                    {
                        trGasComponentMiscDg.Visible = false;
                        trGasComponentMiscTextBox.Visible = true;
                    }
                    
                }
            }
        }
        else
        {
            if (objService.GetGasCompositionMisc(Session["GasCompoMisc"].ToString()).Tables[0].Rows.Count > 0)
            {
                dgGas.DataSource = objService.GetGasCompositionMisc(Session["GasCompoMisc"].ToString());
                dgGas.DataBind();
                Session["GasCompoMisc"] = null;
                trGasComponentMiscDg.Visible = true;
                trGasComponentMiscTextBox.Visible = false;

            }
            else
            {
                trGasComponentMiscDg.Visible = false;
                trGasComponentMiscTextBox.Visible = true;
            }
        }
       

    }

       
    protected void Misscellaneous_CheckedChanged(object sender, EventArgs e)
    {
        if (Miscellaneous.Checked == true)
        {
            trMisscellaneous.Visible = true;
            fillDataGridMisc();
        }
        else
        {
            trMisscellaneous.Visible = false;
        }
    }

    protected void txtGCV_TextChanged(object sender, EventArgs e)
    {

    }

    protected void btnMisscellaneous_Click(object sender, EventArgs e)
    {
        string GasCompId = (string)Session["GasCompId"];

        if (GasCompId == null)
        {
            lblMessage.Text = "First of all you need to add the data for GasComposition";
        }
        else
        {
            if (txtComponent.Text == string.Empty)
            {
                if (txtMiscId.Text == string.Empty)
                {
                    lblMessage.Text = objService.SaveGasComp_Misc(txtComponent.Text, txtRelativeContent.Text, "flase");
                    string GasComMiscId = objService.GetGasCompositionMiscRecentId();
                    Session["GasCompoMisc"] = GasComMiscId;
                    lblMessage.Text = objService.UpdateTable("gascomp", "misc_fk", GasComMiscId, GasCompId);
                    Session["GasCompId"] = null;
                    fillDataGridMisc();
                }
                else
                {
                    if (txtComponent.Text == string.Empty)
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "component", "null", txtMiscId.Text );
                    }
                    else
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "component", txtComponent.Text ,txtMiscId.Text);
                    }

                    if (txtRelativeContent.Text == string.Empty)
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "relativeContent", "null", txtMiscId.Text);
                    }
                    else
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "relativeContent", txtRelativeContent.Text, txtMiscId.Text);
                    }

                    fillDataGridMisc();

                }
            }
            else
            {
                if (txtMiscId.Text == string.Empty)
                {
                    lblMessage.Text = objService.SaveGasComp_Misc(txtComponent.Text, txtRelativeContent.Text, "true");
                    string GasComMiscId = objService.GetGasCompositionMiscRecentId();
                    Session["GasCompoMisc"] = GasComMiscId;
                    lblMessage.Text = objService.UpdateTable("gascomp", "misc_fk", GasComMiscId, GasCompId);
                    Session["GasCompId"] = null;
                    fillDataGridMisc();
                }
                else
                {
                    if (txtComponent.Text == string.Empty)
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "component", "null", txtMiscId.Text);
                    }
                    else
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "component", txtComponent.Text, txtMiscId.Text);
                    }

                    if (txtRelativeContent.Text == string.Empty)
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "relativeContent", "null", txtMiscId.Text);
                    }
                    else
                    {
                        lblMessage.Text = objService.UpdateTable("gascomps_misc", "relativeContent", txtRelativeContent.Text, txtMiscId.Text);
                    }

                    fillDataGridMisc();
                }
            }
        }
    }


    protected void dgGasComp_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgGasComp .Rows[e.NewEditIndex].Cells[1].Text.Trim());
        Session["GasCompId"] = id.ToString();
        fillGasComposition(id);
        trTextboxGasComposition.Visible = true;
        trDgGasComposition.Visible = false;
        btnPrevious.Visible = false;
    }

    private void fillGasComposition(Int32 Id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("gascomp", Id);
        if (ds.Tables[0].Rows.Count > 0)
        {
            txtGasCompId.Text = ds.Tables[0].Rows[0]["id"].ToString();
            txtH2.Text = ds.Tables[0].Rows[0]["H2"].ToString();
            txtCO.Text = ds.Tables[0].Rows[0]["CO"].ToString();
            txtCO2.Text = ds.Tables[0].Rows[0]["CO2"].ToString();
            txtCH4.Text = ds.Tables[0].Rows[0]["CH4"].ToString();
            txtC2H2.Text = ds.Tables[0].Rows[0]["C2H2"].ToString();
            txtC2H4.Text = ds.Tables[0].Rows[0]["C2H4"].ToString();
            txtC2H6.Text = ds.Tables[0].Rows[0]["C2H6"].ToString();
            txtN2.Text = ds.Tables[0].Rows[0]["N2"].ToString();
            txtGCV.Text = ds.Tables[0].Rows[0]["GCV"].ToString();
            txtTar.Text = ds.Tables[0].Rows[0]["Tar"].ToString();
        }     
     }

    private void fillGasCompMisc(Int32 Id)
    {
        DataSet ds = new DataSet();
       

        ds = objService.GetTableDetailsBasedonPK("gascomps_misc", Id);
        txtMiscId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtRelativeContent.Text = ds.Tables[0].Rows[0]["relativeContent"].ToString();
        txtComponent.Text = ds.Tables[0].Rows[0]["component"].ToString();
    }

    private void clearTextBoxes()
    {
        txtGasCompId.Text = string.Empty ;
        txtH2.Text =string.Empty ;
        txtCO.Text =string.Empty ;
        txtCO2.Text = string.Empty ;
        txtCH4.Text = string.Empty ;
        txtC2H2.Text = string.Empty ;
        txtC2H4.Text = string.Empty ;
        txtC2H6.Text = string.Empty ;
        txtN2.Text = string.Empty;
        txtGCV.Text = string.Empty;
        txtTar.Text = string.Empty;
    }

    protected void dgGasComp_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgGasComp.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (txtGasCompId.Text != string.Empty)
        {

            #region Update operation

            if (txtH2.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "H2", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "H2", txtH2.Text, txtGasCompId.Text);
            }

            if (txtCO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CO", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CO", txtCO.Text, txtGasCompId.Text);
            }

            if (txtCO2.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CO2", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CO2", txtCO2.Text, txtGasCompId.Text);
            }

            if (txtGasCompId.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CH4", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "CH4", txtCH4.Text, txtGasCompId.Text);
            }

            if (txtC2H2.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H2", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H2", txtC2H2.Text, txtGasCompId.Text);
            }

            if (txtC2H4.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H4", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H4", txtC2H4.Text, txtGasCompId.Text);
            }

            if (txtC2H6.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H6", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "C2H6", txtC2H6.Text, txtGasCompId.Text);
            }

            if (txtN2.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "N2", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "N2", txtN2.Text, txtGasCompId.Text);
            }

            if (txtGCV.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "GCV","null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "GCV", txtGCV.Text, txtGasCompId.Text);
            }

            if (txtTar.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "Tar", "null", txtGasCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("gascomp", "Tar", txtTar.Text, txtGasCompId.Text);
            }

            #endregion

            fillDataGrid();

            clearTextBoxes();

        }
        else
        {

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpID = (string)Session["ExperimentId"];

                lblMessage.Text = objService.SaveGasComp(txtH2.Text, txtCO.Text, txtCO2.Text, txtCH4.Text, txtC2H2.Text, txtC2H6.Text, txtN2.Text, txtGCV.Text, txtTar.Text,ExpID);

                fillDataGrid();

                clearTextBoxes();
            }
            else
            {
                string RefeId = (string)Session["ReferenceId"];

                objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                //string ExperimentId = objService.GetLastInsertId().ToString();

                string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);

                lblMessage.Text = objService.SaveGasComp(txtH2.Text, txtCO.Text, txtCO2.Text, txtCH4.Text, txtC2H2.Text, txtC2H6.Text, txtN2.Text, txtGCV.Text, txtTar.Text, ExperimentId);
                
                Session["ExperimentId"] = ExperimentId;

                fillDataGrid();

                clearTextBoxes();
            }


            string GasCompositionId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "gascomp");

            Session["GasCompId"] = GasCompositionId;

            if ((string)Session["YieldId"] != null)
            {
                string YieldId = (string)Session["YieldId"];

                lblMessage.Text = "Gas Composition Saved and " + objService.UpdateYield("gasComp_fk", GasCompositionId, YieldId);

                fillDataGrid();


            }
            else
            {
                string ReferenceId = (string)Session["ReferenceId"];

                lblMessage.Text = "Gas Composition Saved and " + objService.SaveYield(null, GasCompositionId,null, null, null, null, null, ReferenceId, (string)Session["ExperimentId"]);

                string YieldId = objService.GetYieldId("gasComp_fk", GasCompositionId);

                Session["YieldId"] = YieldId;

                fillDataGrid();
            }
        }
    }

    protected void dgGasComp_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void dgGas_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgGas.Rows[e.NewEditIndex].Cells[1].Text.Trim());

        fillGasCompMisc(id);
        trGasComponentMiscTextBox.Visible = true;
        trGasComponentMiscDg.Visible = false;
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreGasComposition.aspx");
    }
}
